import 'package:flutter/material.dart';
import 'package:flutterone/common/home_requset.dart';
import 'View/category_page_Item.dart';

import 'Model/json_info_entity.dart';

class CategoryPage extends StatefulWidget {
  CategoryPage({Key? key}) : super(key: key);

  _CategoryPageState createState() => _CategoryPageState();
}

class _CategoryPageState extends State<CategoryPage> {

  @override

  Widget build(BuildContext context) {
    return Container(
        child: Scaffold(appBar: AppBar(
          title: Text('产品分类'),
        ),
          body: ListViewDouban(),
        ));
  }
}
class ListViewDouban extends StatefulWidget {
  @override
  State<ListViewDouban> createState() => _ListViewDoubanState();
}

class _ListViewDoubanState extends State<ListViewDouban> {
  //初始化滚动监听器，加载更多使用
  final ScrollController _controllerListView = ScrollController();
  //保存到数组里
  final List<JsonInfoEntity> jsonInfoEntity = [];

  @override
  void initState() {
    super.initState();
    loadMoreData();
  }

  loadMoreData() async {
    HomeRequset.requsetMovieList(0).then((res) {
      setState(() {
        jsonInfoEntity.addAll(res);
        print("-------${jsonInfoEntity}");
      });
    });
  }

  Future _pullToRefresh() async {
    loadMoreData();
    jsonInfoEntity.clear();
  }

  @override
  Widget build(BuildContext context) {
    return RefreshIndicator(
      color: Colors.cyan,
      onRefresh: _pullToRefresh,//下拉刷新方法
      child: ListView.builder(
          controller: _controllerListView,
          itemCount: jsonInfoEntity.length,
          itemBuilder: (BuildContext ctx, int index) {
            return GestureDetector(
                onTap: () {
                  setState(() {
                    print("${index}");
                    print("${jsonInfoEntity[index].nickname}");
                    if (index == 0) {
                      jsonInfoEntity[1].nickname = "单独的";
                    }
                    jsonInfoEntity[0].nickname = "新设定";
                  });
                },
                child: LDXCategoryItem(jsonInfoEntity[index])
            );
          }
      ),
    );
  }
}
